﻿using System;
using System.Collections.Generic;
using System.Text;
using ToDoList.Logic.DataSource;
using System.Linq;

namespace ToDoList.Logic.Service
{
    public class UserService
    {
        // 注册
        public void Regist(int userID, string passWord)
        {
            if (IsThisUserExisted(userID))
                throw new Exception("This User hava been existed");
            else
                Data.Users.Add(new Users()
                    {
                        UserId = userID,
                        Password = passWord
                    });
        }

        // 登陆
        public bool Login(int userID, string passWord)
        {
            if (!IsThisUserExisted(userID))
                throw new Exception("This User isn't exist");
            else
                return FindUser(userID).Password.Equals(passWord);
        }

        // 新建一个清单
        public void CreateAnInventory(int userID, string inventoryName)
        {
            InventoryService inventoryService = new InventoryService();
            inventoryService.CreateInventory(userID, inventoryName);
        }

        // 新建一个事项
        public void CreateAMatter(int userID, int inventoryID, string title)
        {
            InventoryService inventoryService = new InventoryService();
            inventoryService.AddMatter(userID, inventoryID, title);
        }

        // 查看未完成清单
        public List<Inventories> GetAllInventories(int userID)
        {
            return FindUser(userID).Inventories;
        }

        // 查看所有事项
        public List<Matters> GetAllMatters(int userID)
        {
            return FindUser(userID).Matters;
        }

        // 过期事项提醒
        public string GetExpiredMatter(int userID)
        {
            Users user = FindUser(userID);
            if (user.Matters.Any(matter => !matter.IsDone))
                return "You have a thing need to do";
            else
                return "";
        }

        // 未完成事项统计
        private int NumbersOfUndoEvents(int userID, int inventoryID)
        {
            var matters = FindUser(userID).Matters;
            return matters.Where(m => !m.IsDone).Count();
        }


        // 查看一个事项
        public Matters GetAMatter(int userID, int inventoryID, int matterID)
        {
            try
            {
                return MatterService.FindMatter(userID, inventoryID, matterID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        // 完成一件事
        public bool DoAMatter(int userID, int inventoryID, int matterID)
        {
            if (MatterService.IsThisMatterExisted(userID, inventoryID, matterID))
            {
                MatterService.FindMatter(userID, inventoryID, matterID).IsDone = true;
                return true;
            }
            else
            {
                return false;
            }
        }

        // 取消完成一件事
        public bool CancleAMatter(int userID, int inventoryID, int matterID)
        {
            if (MatterService.IsThisMatterExisted(userID, inventoryID, matterID))
            {
                MatterService.FindMatter(userID, inventoryID, matterID).IsDone = false;
                return true;
            }
            else
            {
                return false;
            }
        }

        // 查看一个清单里的所有事项
        public List<Matters> GetOneOfTheInventory(int userID, int inventoryID)
        {
            if (InventoryService.IsThisInventoryExisted(userID, inventoryID))
                throw new Exception("The inventory isn't exist");
            else
            {
                return InventoryService.FindInventory(userID, inventoryID).Matters;
            }
        }

        // 找到指定的用户
        public static Users FindUser(int userID)
        {
            Users user = Data.Users.SingleOrDefault(u => u.UserId == userID);
            if (user == null)
                throw new Exception("This user isn't exist");
            else
                return user;
        }

        // 指定的用户是否存在
        public static bool IsThisUserExisted(int userID)
        {
            return Data.Users.Any(user => user.UserId == userID);
        }
    }
}
